package com.itheima.admin.controller;

import com.itheima.PageVo;
import com.itheima.Result;
import com.itheima.admin.dto.DeptDto;
import com.itheima.admin.dto.PageDto;
import com.itheima.admin.pojo.Dept;
import com.itheima.admin.service.IDeptService;
import com.itheima.admin.vo.DeptVo;
import com.itheima.admin.vo.RoleVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dept")
@CrossOrigin
public class DeptController {
    @Autowired
    private IDeptService deptService;

    @PostMapping("/list")
    @ApiOperation("部门条件分页查询")
    public PageVo<DeptVo> queryByPage(@RequestBody PageDto pageDto){
        return deptService.queryByPage(pageDto);
    }
    @GetMapping("/listall")
    @ApiOperation("查询所有部门")
    public List<DeptVo> queryAll(){
        return deptService.queryAll();
    }
    @PostMapping("/add")
    @ApiOperation("新增部门")
    public Result insertDept(@RequestBody DeptDto deptDto){
        boolean result = deptService.insertDept(deptDto);
        return new Result(result,result?"部门添加成功":"部门添加失败",null);
    }
    @DeleteMapping("/{id}")
    @ApiOperation("删除部门")
    public Result deleteDept(@PathVariable("id")  String id){
        boolean result = deptService.deleteDept(id);
        return new Result(result,result?"部门删除成功":"部门删除失败",null);
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询部门")
    public DeptDto queryById(@PathVariable("id") String id){
        return deptService.queryById(id);
    }
    @PutMapping("/edit")
    @ApiOperation("修改部门")
    public Result editDept(@RequestBody Dept dept){
        boolean result = deptService.editDept(dept);
        return new Result(result,result?"部门修改成功":"部门修改失败",null);
    }
}
